package com.vitabuybuy.tv.ui.home.bidd

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.ext.setOnclickNoRepeat
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityBiddingBinding
import com.library.common.toast.XToast
import com.library.common.util.DateUtil
import com.vitabuybuy.tv.ui.home.bidd.viewmodel.BiddingZoneViewModel
import com.vitabuybuy.tv.utils.StringUtil
import kotlinx.android.synthetic.main.activity_bidding.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class BiddingActivity : BaseActivity<BiddingZoneViewModel, ActivityBiddingBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_bidding

    private var biddingId = -1

    private var job: Job? = null
    private var timer: CountDownTimer? = null


    override fun onGetBundle(bundle: Bundle?) {
        super.onGetBundle(bundle)
        biddingId = bundle!!.getInt("biddingId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.auction_hall))
        mBinding.viewModel = viewModel
    }

    override fun observeData() {
        job = GlobalScope.launch {
            repeat(1000) {
                viewModel.toAuctionHall(biddingId, it)
                delay(5000L)
            }
        }
        viewModel.auctionHallDto.observe(this, Observer {
            if (viewModel.isFirst.get() == 0) {
                tvPrice.text = StringUtil.to2Double(it.currentPrice).toString()
                countDown(it.remainingTime)
            }
        })
    }

    private fun countDown(remainingTime: Long) {
        timer = object : CountDownTimer(remainingTime * 1000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                tvGroupDeadline.text = String.format(
                    getString(R.string.on_going),
                    DateUtil.formatSeconds(millisUntilFinished / 1000)
                )
            }

            override fun onFinish() {
                tvGroupDeadline.text = getString(R.string.has_over)
            }
        }
        timer!!.start()
    }

    override fun eventHandling() {
        setOnclickNoRepeat(ivMinus, ivAdd, btnBid){v->
            var currentPrice = tvPrice.text.toString().toDouble()
            when (v) {
                ivMinus -> {
                    currentPrice -= viewModel.auctionHallDto.value!!.scope
                    if (currentPrice < 0) {
                        currentPrice = 0.0
                    }
                    tvPrice.text = StringUtil.to2Double(currentPrice).toString()
                }
                ivAdd -> {
                    currentPrice += viewModel.auctionHallDto.value!!.scope
                    if (currentPrice > viewModel.auctionHallDto.value!!.goods.salePrice) {
                        currentPrice = viewModel.auctionHallDto.value!!.goods.salePrice
                    }
                    tvPrice.text = StringUtil.to2Double(currentPrice).toString()
                }
                btnBid -> {
                    //如果开价小于当前价，则为无效开价
                    if (currentPrice < viewModel.auctionHallDto.value!!.currentPrice) {
                        XToast.normal(getString(R.string.please_make_a_new_offer))
                        return@setOnclickNoRepeat
                    }
                    viewModel.toAuction(biddingId, currentPrice)
                }
            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        if (timer != null) {
            timer!!.cancel()
        }
        if (job != null) {
            job!!.cancel()
        }
    }


    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                XToast.success(getString(R.string.already_auction))
                viewModel.toAuctionHall(biddingId, 1)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, biddingId: Int?) {
            mContext.startActivity(
                bundleOf(
                    Pair("biddingId", biddingId)
                ), BiddingActivity::class.java
            )
        }
    }
}
